﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.Utility;
using Aliyeye.Car.Net.UI.Common;

namespace Aliyeye.Car.Net.UI.CarModel
{
    public partial class AddCars : Aliyeye.Car.Net.UI.BaseForm
    {

        CarSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.CarSvc.SoapHeaderClass();
        private bool isDriver = false;
        private int CarsID = 0;
        private int driverID = 0;
        private string driverName = "";
        bool boolresult = false;

        public AddCars()
        {
            InitializeComponent();
        }

        public AddCars(bool isDri)
        {
            InitializeComponent();
            isDriver = isDri;
        }

        public AddCars(bool isDri, int id)
        {
            InitializeComponent();
            isDriver = isDri;
            CarsID = id;
            this.Text = "修改车辆信息";
        }

        private void BindInfo()
        {
            overlay.FadeIn();
            if (CarsID != 0)
            {
                txtSendMile.Visible = false;
                lblsendmile.Visible = false;
            }

            CarSvc.Car model = new Aliyeye.Car.Net.UI.CarSvc.Car();
            ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                proxy => proxy.GetModel(sopaHeader, CarsID),
                result =>
                {
                    if (result.Error == null)
                    {
                        model = (CarSvc.Car)result.Result;
                        if (model != null)
                        {
                            this.txtBuyPrice.Text = model.BuyPrice.ToString();
                            this.txtBuyTime.Value = model.BuyTime;
                            this.txtInsureTime.Value = model.InsureTime;
                            this.txtCartype.Text = model.Cartype;
                            this.txtCode.Text = model.Code.ToString();
                            this.txtColor.Text = model.Color.ToString();
                            this.txtDecorate.Text = model.Decorate.ToString();
                            this.txtdipanCode.Text = model.dipanCode;
                            this.txtDrive.Text = model.Drive == true ? "带驾" : "自驾";
                            this.txtEndbridge.Value = model.Endbridge;
                            this.txtEndOpen.Value = model.EndOpen;
                            this.txtEndCare.Value = model.EndCare;
                            this.txtEndChenshui.Value = model.EndChenshui;
                            this.txtEndOpen.Value = model.EndOpen;
                            this.txtEndyunguan.Value = model.Endyunguan;
                            this.txtFactorCode.Text = model.FactorCode;
                            this.txtInsureEnd.Value = model.InsureEnd;
                            this.txtInsureTime.Value = model.InsureTime;
                            this.txtMotorCode.Text = model.MotorCode;
                            //model.Max = max;
                            this.txtOvertimePrie.Text = model.OvertimePrie.ToString();
                            this.txtOwners.Text = model.Owners;
                            this.txtOwnerTel.Text = model.OwnerTel;
                            this.txtRoadEnd.Value = model.RoadEnd;
                            this.txtRoadTime.Value = model.RoadTime;
                            this.txtSeat.Text = model.Seat;
                            this.textShangpaishui.Text = model.Shangpaishui.ToString();
                            this.txtStarbridge.Value = model.Starbridge;
                            this.txtStarCheshui.Text = model.StarCheshui.ToString();
                            this.txtStarMax.Text = model.StarMax.ToString();
                            this.txtStaryunguan.Value = model.Staryunguan;
                            this.txtTax.Text = model.Tax.ToString();
                            this.cbbBrand.Text = model.Brand;
                            //this.txtUserId.Text = getName(model.UserId);
                            this.dtCarTime.Value = model.FactoryOutDate;
                            this.dtWarrTime.Value = model.WarrTime;
                            this.txtMarks.Text = model.Marks;
                            this.txtTimeoutPrie.Text = model.TimeoutPrie.ToString();
                            this.txtSendMile.Text = model.SendMile.ToString();
                            this.txtNextMile.Text = model.NextMile.ToString();
                            this.txtDayKm.Text = model.DayKm.ToString();
                            this.txtSendMile.Visible = true;
                            this.lblsendmile.Visible = true;
                            this.txtdayrent.Text = model.DayRent.ToString();
                            this.txtMax.Text = model.Max.ToString();

                            this.txtUserId.Text = model.DriverName;
                            driverID = model.UserId;
                            driverName = model.DriverName;

                            if (model.IsSelf)
                                rdbIsSelf.Checked = true;
                            else
                                rdbNoIsSelf.Checked = true;
                            if (model.IsActive)
                                rdbActive.Checked = true;
                            else
                                rdbNoActive.Checked = true;
                        }
                        overlay.FadeOut();
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();
                    }
                },
                this
            );

            //AccountsUsersSvc.Accounts_Users AccountsUserModel = new Aliyeye.Car.Net.UI.AccountsUsersSvc.Accounts_Users();
            //AccountsUsersSvc.SoapHeaderClass sopaHeadercustomer = new Aliyeye.Car.Net.UI.AccountsUsersSvc.SoapHeaderClass();
            //ServiceManipulator.SafeAsyncExecution<AccountsUsersSvc.Accounts_UsersServiceSoapClient>(
            //    proxy => proxy.GetModel(sopaHeadercustomer, model.UserId),
            //    result =>
            //    {
            //        if (result.Error == null)
            //        {
            //            AccountsUserModel = (AccountsUsersSvc.Accounts_Users)result.Result;
            //            if (AccountsUserModel != null)
            //            {
            //                this.txtUserId.Text = AccountsUserModel.TrueName;

            //            }

            //            overlay.FadeOut();
            //        }
            //        else
            //        {
            //            PageCommon.Error(result.Error.Message); overlay.FadeOut();
            //        }
            //    },
            //    this
            //);
        }



        private void AddCars_Load(object sender, EventArgs e)
        {
            BindComboBoxInfo();
            if (CarsID != 0)
                BindInfo();
        }

        private void BindComboBoxInfo()
        {
            this.txtDrive.SelectedIndex = 0;

            //txtCartype 车型
            //txtSeat  座位数，型号
            //cbbBrand  厂牌

            overlay.FadeIn();
            ArchivesSvc.SoapHeaderClass sopaHeader = new ArchivesSvc.SoapHeaderClass();

            DataSet dsCarType = null;
            ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.ComBoxCar(sopaHeader),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsCarType = (DataSet)result.Result;
                        txtCartype.DisplayMember = "Namevalue";
                        txtCartype.ValueMember = "Id";
                        txtCartype.DataSource = dsCarType.Tables[0];
                        txtCartype.SelectedIndex = 0;

                        overlay.FadeOut();
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();
                    }
                },
                this
            );

            DataSet dsCarSeat = null;
            ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.ComBoxSit(sopaHeader),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsCarSeat = (DataSet)result.Result;
                        txtSeat.DisplayMember = "Namevalue";
                        txtSeat.ValueMember = "Id";
                        txtSeat.DataSource = dsCarSeat.Tables[0];
                        txtSeat.SelectedIndex = 0;

                        overlay.FadeOut();
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();
                    }
                },
                this
            );

            DataSet dsBrand = null;
            ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.ComBoxBrand(sopaHeader),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsBrand = (DataSet)result.Result;
                        if (dsBrand != null && dsBrand.Tables[0].Rows.Count > 0)
                        {
                            cbbBrand.DisplayMember = "Namevalue";
                            cbbBrand.ValueMember = "Id";
                            cbbBrand.DataSource = dsBrand.Tables[0];
                            cbbBrand.SelectedIndex = 0;
                        }
                        overlay.FadeOut();

                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();
                    }
                },
                this
            );


            DataSet dsColor = null;
            ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.ComBoxColor(sopaHeader),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsColor = (DataSet)result.Result;
                        if (dsColor != null && dsColor.Tables[0].Rows.Count > 0)
                        {
                            txtColor.DisplayMember = "Namevalue";
                            txtColor.ValueMember = "Id";
                            txtColor.DataSource = dsColor.Tables[0];
                            txtColor.SelectedIndex = 0;
                        }
                        overlay.FadeOut();

                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();
                    }
                },
                this
            );


        }

        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            int max = 0;
            int txtStarMax = 0;
            int txtDayKm = 0;
            int txtSendMile = 0;
            Int64 txtNextMile = 0;
            decimal txtOvertimePrie = 0;
            decimal textShangpaishui = 0;
            decimal txtTax = 0;
            decimal txtBuyPrice = 0;
            decimal txtTimeoutPrie = 0;
            decimal txtDayRent = 0;



            #region 输入的格式判断
            if (string.IsNullOrEmpty(this.txtCode.Text))
            {
                DialogWarning dia = new DialogWarning("车牌号不能为空！");
                dia.ShowDialog();
                return;
            }

            if (string.IsNullOrEmpty(this.txtdayrent.Text) && this.txtDrive.Text.Trim() == "自驾")
            {
                DialogWarning dia = new DialogWarning("每日租金数不能为空！");
                dia.ShowDialog();
                return;
            }

            if (string.IsNullOrEmpty(txtUserId.Text))
            {
                DialogWarning dia = new DialogWarning("驾驶员不能为空！");
                dia.ShowDialog();
                return;

            }
            else
            {
                if (driverID == 0)
                {
                    DialogWarning dia = new DialogWarning("填入的驾驶员不存在，请填写正确的驾驶员名！");
                    dia.ShowDialog();
                    return;
                }

                //IsexistDirve(txtUserId.Text.Trim());
            }



            if (!string.IsNullOrEmpty(this.txtMax.Text))
            {
                try
                {

                    max = Convert.ToInt32(this.txtMax.Text);
                    if (max < 0)
                    {
                        DialogWarning dia = new DialogWarning("当前公里数不能为负数！");
                        dia.ShowDialog();
                        return;

                    }
                }

                catch
                {
                    DialogWarning dia = new DialogWarning("当前公里数应该为整数！");
                    dia.ShowDialog();
                    return;
                }
            }

            if (!string.IsNullOrEmpty(this.txtStarMax.Text))
            {
                try
                {
                    txtStarMax = Convert.ToInt32(this.txtStarMax.Text);
                    if (max < 0)
                    {
                        DialogWarning dia = new DialogWarning("起始公里不能为负数！");
                        dia.ShowDialog();
                        return;

                    }
                }
                catch
                {
                    DialogWarning dia = new DialogWarning("起始公里数应该为整数！");
                    dia.ShowDialog();
                    return;
                }
            }

            if (!string.IsNullOrEmpty(this.txtBuyPrice.Text))
            {

                try
                {
                    txtBuyPrice = Convert.ToDecimal(this.txtBuyPrice.Text);
                    if (max < 0)
                    {
                        DialogWarning dia = new DialogWarning("购买价格不能为负数！");
                        dia.ShowDialog();
                        return;

                    }
                }
                catch
                {
                    DialogWarning dia = new DialogWarning("请填入正确的购买价格！");
                    dia.ShowDialog();
                    return;
                }
            }

            if (!string.IsNullOrEmpty(this.txtTax.Text))
            {
                try
                {
                    txtTax = Convert.ToDecimal(this.txtTax.Text);
                    if (max < 0)
                    {
                        DialogWarning dia = new DialogWarning("购置税不能为负数！");
                        dia.ShowDialog();
                        return;

                    }
                }
                catch
                {
                    DialogWarning dia = new DialogWarning("请填入正确的购置税！");
                    dia.ShowDialog();
                    return;
                }
            }

            if (!string.IsNullOrEmpty(this.txtOvertimePrie.Text))
            {
                try
                {
                    textShangpaishui = Convert.ToDecimal(this.txtOvertimePrie.Text);
                    if (max < 0)
                    {
                        DialogWarning dia = new DialogWarning("超驶收费不能为负数！");
                        dia.ShowDialog();
                        return;

                    }
                }
                catch
                {
                    DialogWarning dia = new DialogWarning("请填入正确的超驶收费！");
                    dia.ShowDialog();
                    return;
                }
            }


            if (!string.IsNullOrEmpty(this.txtTimeoutPrie.Text))
            {
                try
                {
                    txtTimeoutPrie = Convert.ToDecimal(this.txtTimeoutPrie.Text);
                    if (max < 0)
                    {
                        DialogWarning dia = new DialogWarning("超时收费不能为负数！");
                        dia.ShowDialog();
                        return;

                    }
                }
                catch
                {
                    DialogWarning dia = new DialogWarning("请填入正确超时收费！");
                    dia.ShowDialog();
                    return;
                }
            }


            if (!string.IsNullOrEmpty(this.txtDayKm.Text))
            {
                try
                {
                    txtDayKm = Convert.ToInt32(this.txtDayKm.Text);
                    if (max < 0)
                    {
                        DialogWarning dia = new DialogWarning("每天限时里程不能为负数！");
                        dia.ShowDialog();
                        return;

                    }
                }
                catch
                {
                    DialogWarning dia = new DialogWarning("请填入正确每天限时里程！");
                    dia.ShowDialog();
                    return;
                }
            }


            if (!string.IsNullOrEmpty(this.txtdayrent.Text) && this.txtDrive.Text.Trim() == "自驾")
            {
                try
                {
                    txtDayRent = Convert.ToDecimal(this.txtdayrent.Text);
                    if (max < 0)
                    {
                        DialogWarning dia = new DialogWarning("每日租金不能为负数！");
                        dia.ShowDialog();
                        return;

                    }
                }
                catch
                {
                    DialogWarning dia = new DialogWarning("请填入正确每日租金！");
                    dia.ShowDialog();
                    return;
                }
            }

            if (!string.IsNullOrEmpty(this.txtSendMile.Text))
            {
                try
                {
                    txtSendMile = Convert.ToInt16(this.txtSendMile.Text);
                    if (max < 0)
                    {
                        DialogWarning dia = new DialogWarning("二保里程数不能为负数！");
                        dia.ShowDialog();
                        return;

                    }
                }
                catch
                {
                    DialogWarning dia = new DialogWarning("请填入正确二保里程数！");
                    dia.ShowDialog();
                    return;
                }
            }

            if (!string.IsNullOrEmpty(this.txtNextMile.Text))
            {
                try
                {
                    txtNextMile = Convert.ToInt64(this.txtNextMile.Text);
                    if (max < 0)
                    {
                        DialogWarning dia = new DialogWarning("下次二保数不能为负数！");
                        dia.ShowDialog();
                        return;

                    }
                }
                catch
                {
                    DialogWarning dia = new DialogWarning("请填入正确下次二保数！");
                    dia.ShowDialog();
                    return;
                }
            }

            if (!string.IsNullOrEmpty(this.textShangpaishui.Text))
            {
                try
                {
                    textShangpaishui = Convert.ToDecimal(this.textShangpaishui.Text);
                    if (max < 0)
                    {
                        DialogWarning dia = new DialogWarning("上牌费不能为负数！");
                        dia.ShowDialog();
                        return;

                    }
                }
                catch
                {
                    DialogWarning dia = new DialogWarning("请填入正确上牌费！");
                    dia.ShowDialog();
                    return;
                }
            }


            #endregion 输入的格式判断
            /****输入保存****/
            CarSvc.Car info = new Aliyeye.Car.Net.UI.CarSvc.Car();
            info.BuyPrice = txtBuyPrice;
            info.BuyTime = this.txtBuyTime.Value;
            info.CareTime = this.txtInsureTime.Value;
            info.Cartype = this.txtCartype.Text;
            info.Code = this.txtCode.Text;
            info.Color = this.txtColor.Text;
            info.Decorate = this.txtDecorate.Text;
            info.dipanCode = this.txtdipanCode.Text;
            info.Drive = Convert.ToBoolean(this.txtDrive.SelectedIndex);
            info.Endbridge = this.txtEndbridge.Value;
            info.EndCard = this.txtEndOpen.Value;
            info.EndCare = this.txtEndCare.Value;
            info.EndChenshui = this.txtEndChenshui.Value;
            info.EndOpen = this.txtEndOpen.Value;
            info.EndPass = DateTime.Now;
            info.Path = string.Empty;
            info.Endyunguan = this.txtEndyunguan.Value;
            info.FactorCode = this.txtFactorCode.Text;
            info.InsureEnd = this.txtInsureEnd.Value;
            info.InsureTime = this.txtInsureTime.Value;
            info.Max = max;
            info.MotorCode = this.txtMotorCode.Text;
            info.OvertimePrie = txtOvertimePrie;
            info.Owners = this.txtOwners.Text;
            info.OwnerTel = this.txtOwnerTel.Text;
            info.RoadEnd = this.txtRoadEnd.Value;
            info.RoadTime = this.txtRoadTime.Value;
            info.Seat = this.txtSeat.Text;
            info.Shangpaishui = textShangpaishui;
            info.Starbridge = this.txtStarbridge.Value;
            info.StarCard = DateTime.Now;
            info.StarCheshui = this.txtStarCheshui.Value;
            info.StarMax = txtStarMax;
            info.StarOPen = this.txtStarOpen.Value;
            info.StarPasTime = DateTime.Now;
            info.Staryunguan = this.txtStaryunguan.Value;
            info.Tax = txtTax;
            info.Marks = this.txtMarks.Text;
            info.Brand = cbbBrand.Text;
            info.WarrTime = this.dtWarrTime.Value;
            info.FactoryOutDate = this.dtCarTime.Value;
            info.CarTime = this.dtCarTime.Value;
            info.TimeoutPrie = txtTimeoutPrie;
            info.DayKm = txtDayKm;
            info.DayRent = txtDayRent;
            info.UserId = driverID;
            info.CarKind = string.Empty;
            info.SendMile = txtSendMile;
            info.NextMile = txtNextMile;
            info.IsSelf = rdbIsSelf.Checked;
            info.IsActive = rdbActive.Checked;
            info.ShangPaiFei = textShangpaishui;
            info.BuyPrice = txtBuyPrice;

            if (CarsID == 0)
            {
                //添加
                ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                    proxy => proxy.Add(sopaHeader, info),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            PageCommon.Notify("添加车辆信息成功！");
                            overlay.FadeOut();
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut();
                        }
                    },
                    this
                );
                ClearControlText();
            }
            else
            {
                //修改
                info.Id = CarsID;
                ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                proxy => proxy.Update(sopaHeader, info),
                result =>
                {
                    if (result.Error == null)
                    {
                        PageCommon.Notify("修改车辆信息成功！");
                        overlay.FadeOut();
                        this.Close();
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();
                    }
                },
                this
            );
            }

            ClearControlText();
        }

        private void ClearControlText()
        {
            foreach (Control cont in this.groupBox1.Controls)
            {
                if (cont is TextBox)
                {
                    cont.Text = "";
                }
            }
        }

        #region 驾驶员输入框数据绑定


        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strSearch = this.txtUserId.Text;
                if (string.IsNullOrEmpty(strSearch))
                {
                    DialogWarning dia = new DialogWarning("驾驶员姓名不能为空！");
                    dia.ShowDialog();
                    return;
                }
                else
                {
                    DisplayCustomeLIst(strSearch, "姓名");
                }
            }
        }

        private void DisplayCustomeLIst(string strSearch, string type)
        {
            if (string.IsNullOrEmpty(strSearch))
            {
                return;
            }
            string strSelectWhere = "";
            if (type == "姓名")
            {
                strSelectWhere = " TrueName like '%" + strSearch + "%'";
            }
            DataSet ds = null;
            dgvInfo.DataSource = null;
            dgvInfo.AutoGenerateColumns = false;
            AccountsUsersSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.AccountsUsersSvc.SoapHeaderClass();
            ServiceManipulator.SafeAsyncExecution<AccountsUsersSvc.Accounts_UsersServiceSoapClient>(
                proxy => proxy.GetDriveList(sopaHeader, strSelectWhere),
                result =>
                {
                    if (result.Error == null)
                    {
                        ds = (DataSet)result.Result;
                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            dgvInfo.DataSource = ds.Tables[0];
                            this.dgvInfo.Visible = true;
                            this.dgvInfo.Focus();
                        }
                        else
                        {
                            PageCommon.Alert("查找的驾驶员不存在，请重新查询！");
                            txtUserId.Text = driverName;
                        }
                        overlay.FadeOut();
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();
                    }
                },
                this
            );

        }

        private void dgvInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvInfo.Rows.Count; )
                {
                    this.dgvInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        private void dgvInfo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                this.txtUserId.Text = this.dgvInfo.CurrentRow.Cells["姓名"].Value.ToString();
                driverID = Convert.ToInt32(this.dgvInfo.CurrentRow.Cells["编号"].Value.ToString());
                driverName = this.txtUserId.Text;
                this.dgvInfo.Visible = false;
                this.txtColor.Focus();
            }
        }

        #endregion 驾驶员输入框数据绑定
        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtDrive_TextChanged(object sender, EventArgs e)
        {

            if (this.txtDrive.Text.Trim() == "自驾")
            {
                this.txtdayrent.Visible = true;
                this.lbltxtdayrent.Visible = true;
            }
            else
            {
                this.txtdayrent.Visible = false;
                this.lbltxtdayrent.Visible = false;

            }
        }
        //判断驾驶员是否存在
        private void IsexistDirve(string strSearch)
        {
            AccountsUsersSvc.Accounts_Users model = new Aliyeye.Car.Net.UI.AccountsUsersSvc.Accounts_Users();
            AccountsUsersSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.AccountsUsersSvc.SoapHeaderClass();
            ServiceManipulator.SafeAsyncExecution<AccountsUsersSvc.Accounts_UsersServiceSoapClient>(
                proxy => proxy.GetModel(sopaHeader, driverID),
                result =>
                {
                    if (result.Error == null)
                    {
                        model = (AccountsUsersSvc.Accounts_Users)result.Result;
                        if (model.TrueName != strSearch)
                        {
                            boolresult = true;
                            PageCommon.Notify("填入的驾驶员不存在，请填写正确的驾驶员名！");
                            overlay.FadeOut();
                        }

                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();
                    }
                },
                this
            );

        }



    }
}
